#include <algorithm>
#include <iostream>
#define LL long long
using namespace std;

const int kMaxN = 2e5 + 10;

int cnt[kMaxN], a[kMaxN];

int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int T;
  for (cin >> T; T; T--) {
    int n, k;
    bool fail = false;
    LL ans = 0;

    cin >> n >> k;
    fill(cnt, cnt + n + 1, 0);
    for (int i = 0; i < n; i++) {
      cin >> a[i];
      cnt[a[i]]++;
    }
    for (int i = 1; i <= n; i++) {
      if (cnt[i] % k == 0) {
        cnt[i] /= k;
      } else {
        fail = true;
        break;
      }
    }
    if (!fail) {
      for (int i = 0, j = 0; i < n; i++) {
        for (; j < n; j++) {
          if (cnt[a[j]] <= 0) {
            break;
          }
          cnt[a[j]]--;
        }
        cnt[a[i]]++;
        ans += j - i;
      }
    }
    cout << ans << "\n";
  }
  return 0;
}